package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.SiteDao;
import com.bestv.search.common.model.Site;

import java.util.List;

public class SiteDaoHibernate extends GenericDaoHibernate<Site, Long> implements SiteDao {

    public SiteDaoHibernate() {
        super(Site.class);
    }

    @SuppressWarnings("unchecked")
    public Site getSiteByCode(String code) {
        List<Site> sites = super.getHibernateTemplate().find("from Site s where s.code = ?", code);
        if (sites != null && sites.size() > 0)
            return sites.get(0);
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Site> getSitesByCodes(String[] codes) {
        StringBuilder sql = new StringBuilder("from Site s where ");
        if (codes != null && codes.length > 0) {
            for (int i = 0; i < codes.length; i++) {
                sql.append("s.code = ?");
                if (i < codes.length - 1) {
                    sql.append(" or ");
                }
            }
        }

        List<Site> sites = super.getHibernateTemplate().find(sql.toString(), codes);
        if (sites != null && sites.size() > 0)
            return sites;
        return null;
    }

    public Site getSiteById(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressWarnings("unchecked")
    public Site getSiteByName(String name) {
        // TODO Auto-generated method stub
        List<Site> sites = super.getHibernateTemplate().find("from Site s where s.name = ?", name);
        if (sites != null && sites.size() > 0)
            return sites.get(0);
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Site> getSites() {
        return super.getHibernateTemplate().find("from Site");
    }

    public Site saveSite(Site site) {
        // TODO Auto-generated method stub
        return super.save(site);
    }

    @SuppressWarnings("unchecked")
    public List<Site> getSitesExpHDQ() {
        return super.getHibernateTemplate().find("from Site s where s.code != ?", "HDQ");
    }
}
